package a09_贪心算法;

import java.util.Arrays;

/**
 * <p>
 * a02_分发饼干复习0
 * </p>
 *
 * @author flyduck
 * @since 2025/1/10
 */
public class a02_分发饼干复习0 {
    //局部最优：饭量最大的分最大饼干
    public int findContentChildren(int[] children, int[] foods) {
        if (foods.length == 0) {
            return 0;
        }
        Arrays.sort(children);
        Arrays.sort(foods);
        int foodIdx = foods.length - 1;
        for (int childIdx = children.length - 1; childIdx >= 0; childIdx--) {
            if(foods[foodIdx] >= children[childIdx]){//够吃
                foodIdx--;//吃过了其他孩子不能吃了
            }
            if(foodIdx < 0){//没有吃的了
                break;
            }
        }
        return foods.length - 1 - foodIdx;
    }
}
